package com.meixiang.fragment.service;


import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.homes.service.ServiceDetailActivity;
import com.meixiang.adapter.service.GoodAtServiceAdapter;
import com.meixiang.entity.services.GoodAtList;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.recyclerview.OnItemClick;
import com.meixiang.recyclerview.RecycleViewDivider;
import com.meixiang.util.AbJsonUtil;
import com.meixiang.util.AbToastUtil;
import com.meixiang.view.swipetoloadlayout.base.OnLoadMoreListener;
import com.meixiang.view.swipetoloadlayout.base.OnRefreshListener;
import com.meixiang.view.swipetoloadlayout.base.SwipeToLoadLayout;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 擅长服务
 * xiejingwen
 */
public class GoodAtServiceFragment extends BaseFragment implements OnRefreshListener,OnLoadMoreListener{
    private View view;

    @Bind(R.id.swipe_target)
    RecyclerView swipeTarget;
    @Bind(R.id.refresh)
    SwipeToLoadLayout refresh;
    private GoodAtServiceAdapter adapter;
    private GoodAtList goodAtList;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_good_at, container, false);
        ButterKnife.bind(this, view);
        this.view=view;
        return view;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        refresh.setOnRefreshListener(this);
        refresh.setLoadMoreEnabled(false);
        swipeTarget.setLayoutManager(new LinearLayoutManager(getActivity()));
        swipeTarget.addItemDecoration(new RecycleViewDivider(getActivity(), RecycleViewDivider.HORIZONTAL_LIST));
        adapter=new GoodAtServiceAdapter(getActivity());
        swipeTarget.setAdapter(adapter);
        adapter.setOnItemClickListener(new OnItemClick() {
            @Override
            public void onItemClick(View view, int position) {
                Bundle bundle=new Bundle();
                bundle.putString("goodsId",adapter.data.get(position).getGoodsId());
                startActivity(bundle, ServiceDetailActivity.class);
            }
        });
    }

    @Override
    protected void setData() {
        showLoading();
        getData(1,true);
    }
    public void getData(int pageNo, final boolean isPull){
        HttpParams params=new HttpParams();
        params.put("beauticianId",activity.getIntent().getStringExtra("beauticianId"));
        params.put("pageNo",pageNo+"");
        HttpUtils.post(Config.BEAUTICIAN_GOOD_AT_LIST, params, new HttpCallBack(activity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                if (isPull){
                    adapter.clearData();
                }
                goodAtList= AbJsonUtil.fromJson(result.toString(),GoodAtList.class);
                if (goodAtList!=null&&goodAtList.getGoodAtList().size()>0){
                    removeStatus();
                    adapter.addData(goodAtList.getGoodAtList());
                }else{
                    showNoData("暂无数据");
                }
            }

            @Override
            public void onError(String code, String message) {
                showNoData(message, "重新加载", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showLoading();
                        getData(1,true);
                    }
                });
            }

            @Override
            public void onAfter(boolean isFromCache, JSONObject jsonObject, Call call, Response response, Exception e) {
                super.onAfter(isFromCache, jsonObject, call, response, e);
                onComplete(refresh);
            }
        });
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onLoadMore() {
        if (goodAtList.getTotalPage()>=goodAtList.getPageNo()){
            AbToastUtil.showToast(getActivity(), "已没有更多数据");
            onComplete(refresh);
        }else{
            int pageNo=goodAtList.getPageNo();
            pageNo++;
            getData(pageNo,false);
        }
    }

    @Override
    public void onRefresh() {
        getData(1,true);
    }

}
